/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package perl;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import javax.swing.*;
import org.w3c.dom.*;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.w3c.dom.NodeList;

/**
 * Cette classe qui hérite de la classe JDialog définit une dialogue box contenant des propriétés du profil.
 * @author NGUYEN Dac Cong Tai
 */
public class PerlProperties extends JDialog{
    private JButton closeButton;
    private JPanel classLabel;
    private int nombreCol;
    private int nombreID;
    private JScrollPane scrollPane;

    /**
     * Constructeur avec 3 paramètres.
     * @param element l'élément contenant des informations d'application
     * @param url le chemin du profil
     * @param langue le langage d'affichage
     * @throws UnsupportedEncodingException
     */
    public PerlProperties(Element element, String url, String langue, String typeProfil) throws UnsupportedEncodingException {
        Element properties = (Element) element.getElementsByTagName("FProprietes").item(0);
        String caption = ((Element)properties.getElementsByTagName("Proprietes").item(0)).getElementsByTagName(langue).item(0).getTextContent();
        setTitle(caption);
        initComponents( properties, url, langue, typeProfil);
        getRootPane().setDefaultButton(closeButton);
        Dimension ecran = Toolkit.getDefaultToolkit().getScreenSize();
        int width = getWidth();

        if((width-ecran.width)>=0){
            int col = (width-150)/nombreCol;
            nombreCol--;
            while (col*nombreCol>(ecran.width-150)){
                nombreCol--;
            }
            classLabel.setLayout(new GridLayout(0, nombreCol, 3, 0));
            if(nombreID>nombreCol*20){
                scrollPane.setPreferredSize(new Dimension(col*nombreCol, 296));
            }
        }
    }

    /**
     * Action de la fermeture.
     */
    @Action
    public void closeBox() {
        dispose();
    }

    /**
     * Initialise le dialogue box.
     * @param element l'élément contenant des informations d'application
     * @param url le chemin du profil
     * @param codeLangue le langage d'affichage
     * @throws UnsupportedEncodingException
     */
    private void initComponents(Element element, String url, String codeLangue, String typeProfil) throws UnsupportedEncodingException {
        Element rootEl = null;
        try {
            rootEl = Utility.getElement(url);
        } catch (Exception ex) {
            System.out.println("Erreur pendant la conversion: "+ex);
        }

        closeButton = new JButton();
        JLabel appTitleLabel = new JLabel();
        JLabel structureLabel = new JLabel();
        JLabel profilStructureLabel = new JLabel();
        JLabel creatorLabel = new JLabel();
        JLabel profilCreatorLabel = new JLabel();
        JLabel locationLabel = new JLabel();
        JLabel profilLocationLabel = new JLabel();
        JLabel createdLabel = new JLabel();
        JLabel profilCreatedLabel = new JLabel();
        JLabel modifiedLabel = new JLabel();
        JLabel profilModifiedLabel = new JLabel();
        JLabel imageLabel = new JLabel();

        setModal(true);
        setName("propertieBox"); // NOI18N
        setResizable(false);

        ActionMap actionMap = Application.getInstance(perl.App.class).getContext().getActionMap(this);
        closeButton.setAction(actionMap.get("closeBox")); // NOI18N
        closeButton.setText(((Element)element.getElementsByTagName("Fermer").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent());
        closeButton.setName("closeButton"); // NOI18N

        appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD, appTitleLabel.getFont().getSize()+4));

        structureLabel.setFont(structureLabel.getFont().deriveFont(structureLabel.getFont().getStyle() | java.awt.Font.BOLD));
        structureLabel.setText(((Element)element.getElementsByTagName("StructureProfil").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()); // NOI18N
        structureLabel.setName("structureLabel"); // NOI18N

        profilStructureLabel.setText(rootEl.getAttribute("nom")); // NOI18N
        profilStructureLabel.setName("profilStructureLabel"); // NOI18N

        creatorLabel.setFont(creatorLabel.getFont().deriveFont(creatorLabel.getFont().getStyle() | java.awt.Font.BOLD));
        creatorLabel.setText(((Element)element.getElementsByTagName("Createur").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()); // NOI18N // NOI18N
        creatorLabel.setName("creatorLabel"); // NOI18N

        profilCreatorLabel.setText(rootEl.getAttribute("createur")); // NOI18N
        profilCreatorLabel.setName("profilCreatorLabel"); // NOI18N

        locationLabel.setFont(locationLabel.getFont().deriveFont(locationLabel.getFont().getStyle() | java.awt.Font.BOLD));
        locationLabel.setText(((Element)element.getElementsByTagName("Emplacement").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()); // NOI18N // NOI18N
        locationLabel.setName("locationLabel"); // NOI18N

        profilLocationLabel.setText(url); // NOI18N
        profilLocationLabel.setName("profilLocationLabel"); // NOI18N

        createdLabel.setFont(createdLabel.getFont().deriveFont(createdLabel.getFont().getStyle() | java.awt.Font.BOLD));
        createdLabel.setText(((Element)element.getElementsByTagName("Creation").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()); // NOI18N // NOI18N
        createdLabel.setName("createdLabel"); // NOI18N

        profilCreatedLabel.setText(rootEl.getAttribute("date_creation")); // NOI18N
        profilCreatedLabel.setName("profilCreatedLabel"); // NOI18N

        modifiedLabel.setFont(modifiedLabel.getFont().deriveFont(modifiedLabel.getFont().getStyle() | java.awt.Font.BOLD));
        modifiedLabel.setText(((Element)element.getElementsByTagName("DernierModification").item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()); // NOI18N // NOI18N
        modifiedLabel.setName("modifiedLabel"); // NOI18N

        profilModifiedLabel.setText(rootEl.getAttribute("date_derniere_modif")); // NOI18N
        profilModifiedLabel.setName("profilModifiedLabel"); // NOI18N

        ImageIcon im = new ImageIcon (Toolkit.getDefaultToolkit().getImage(Utility.getPath(this,"/Fichiers systeme/Images/about.png")));
        imageLabel.setIcon(im);
        imageLabel.setName("imageLabel"); // NOI18N

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        if(typeProfil.equals("Eleve")){
            appTitleLabel.setText(((Element)element.getElementsByTagName(typeProfil).item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()
                                +" "+rootEl.getAttribute("nom_eleve")+" "+rootEl.getAttribute("prenom_eleve")); // NOI18N
            appTitleLabel.setName("appTitleLabel"); // NOI18N
            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(imageLabel)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(structureLabel)
                                .addComponent(creatorLabel)
                                .addComponent(locationLabel)
                                .addComponent(createdLabel)
                                .addComponent(modifiedLabel))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(profilStructureLabel)
                                .addComponent(profilCreatorLabel)
                                .addComponent(profilLocationLabel)
                                .addComponent(profilCreatedLabel)
                                .addComponent(profilModifiedLabel)))
                        .addComponent(appTitleLabel, GroupLayout.Alignment.LEADING)
                        .addComponent(closeButton))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(imageLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(appTitleLabel)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(structureLabel)
                        .addComponent(profilStructureLabel))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(creatorLabel)
                        .addComponent(profilCreatorLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(locationLabel)
                        .addComponent(profilLocationLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(createdLabel)
                        .addComponent(profilCreatedLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(modifiedLabel)
                        .addComponent(profilModifiedLabel))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                    .addComponent(closeButton)
                    .addContainerGap())
            );
        } else if (typeProfil.equals("Groupe")){
	    Element eleves = (Element) rootEl.getElementsByTagName("groupe").item(0);
            appTitleLabel.setText(((Element)element.getElementsByTagName(typeProfil).item(0)).getElementsByTagName(codeLangue).item(0).getTextContent()
                                +" "+eleves.getAttribute("nom")); // NOI18N
            appTitleLabel.setName("appTitleLabel"); // NOI18N
            nombreCol=url.length()*7/150;
            if(nombreCol<1)
                nombreCol=1;
            
            classLabel = new JPanel(new GridLayout(0, nombreCol, 3, 0));
            nombreID=0;
            
                Element classesEprofilea = Utility.getElement(Utility.getPath(this, "/Fichiers systeme/Parametres/Classes.xml"));
                NodeList listEleves = classesEprofilea.getElementsByTagName("Eleve");
                
                NodeList listElevesDansGroup = eleves.getElementsByTagName("eleve");
                ArrayList listID = new ArrayList();
                for (int i=0; i<listElevesDansGroup.getLength(); i++){
                    listID.add(((Element)listElevesDansGroup.item(i)).getAttribute("id"));
                }
                nombreID=listID.size();
                for(int j=0; j<listEleves.getLength(); j++){
                    if(listID.size()>0){
                        if(listID.contains(((Element)listEleves.item(j)).getAttribute("id"))){
                            JLabel eleveTmp = new JLabel(((Element)listEleves.item(j)).getAttribute("nom").toUpperCase()+" "+((Element)listEleves.item(j)).getAttribute("prenom"));
                            classLabel.add(eleveTmp);
                            listID.remove(((Element)listEleves.item(j)).getAttribute("id"));
                        }
                    }else
                        break;
                }

            scrollPane = new JScrollPane(classLabel);
            scrollPane.setBorder(null);
            if(nombreID>nombreCol*20){
                scrollPane.setPreferredSize(new Dimension(GroupLayout.DEFAULT_SIZE, 296));
            }

            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(imageLabel)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(structureLabel)
                                .addComponent(creatorLabel)
                                .addComponent(locationLabel)
                                .addComponent(createdLabel)
                                .addComponent(modifiedLabel))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(profilStructureLabel)
                                .addComponent(profilCreatorLabel)
                                .addComponent(profilLocationLabel)
                                .addComponent(profilCreatedLabel)
                                .addComponent(profilModifiedLabel)))
                        .addComponent(appTitleLabel, GroupLayout.Alignment.LEADING)
                        .addGap(18, 18, 18)
                        .addComponent(scrollPane, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(closeButton))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(imageLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(appTitleLabel)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollPane)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(structureLabel)
                        .addComponent(profilStructureLabel))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(creatorLabel)
                        .addComponent(profilCreatorLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(locationLabel)
                        .addComponent(profilLocationLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(createdLabel)
                        .addComponent(profilCreatedLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(modifiedLabel)
                        .addComponent(profilModifiedLabel))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                    .addComponent(closeButton)
                    .addContainerGap())
            );
        }
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
    }
}
